package com.syyo.gulimall.cart.vo;

import java.math.BigDecimal;
import java.util.List;

/**
 * @ author: wang
 * @ date: 2021/4/20 14:46
 * @ Description: 需要计算的属性，重写它的get方法
 */
public class Cart {

    private List<CartItem> items;// 商品
    private Integer countNum;//商品总数量
    private Integer countType;//商品总类型
    private BigDecimal totalAmount;//商品总价
    private BigDecimal reduce = new BigDecimal("0.00");//减免价

    public Cart() {
    }

    public Cart(List<CartItem> items, Integer countNum, Integer countType, BigDecimal totalAmount, BigDecimal reduce) {
        this.items = items;
        this.countNum = countNum;
        this.countType = countType;
        this.totalAmount = totalAmount;
        this.reduce = reduce;
    }

    public List<CartItem> getItems() {
        return items;
    }

    public void setItems(List<CartItem> items) {
        this.items = items;
    }

    public Integer getCountNum() {
        Integer count = 0;
        if (this.items != null && this.items.size() > 0) {
            for (CartItem item : this.items) {
                count += item.getCount();
            }
        }
        return count;
    }

    public void setCountNum(Integer countNum) {
        this.countNum = countNum;
    }

    public Integer getCountType() {
        Integer count = 0;
        if (this.items != null && this.items.size() > 0) {
            for (CartItem item : this.items) {
                count += 1;
            }
        }
        return count;
    }

    public void setCountType(Integer countType) {
        this.countType = countType;
    }

    public BigDecimal getTotalAmount() {
        BigDecimal totalPrice = new BigDecimal("0.00");
        // 1.计算购物量总价
        if (this.items != null && this.items.size() > 0) {
            for (CartItem item : this.items) {
                if (item.getCheck()) {
                    BigDecimal itemPrice = item.getTotalPrice();
                    totalPrice = totalPrice.add(itemPrice);
                }

            }
        }
        // 2.减去优惠价
        BigDecimal subtract = totalPrice.subtract(getReduce());

        return subtract;
    }

    public void setTotalAmount(BigDecimal totalAmount) {
        this.totalAmount = totalAmount;
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }
}
